/*
 * @Author: nns4
 * @Date: 2019-10-31 17:30:17
 * @LastEditors: nns4
 * @LastEditTime: 2020-12-03 14:14:34
 * @FilePath: /studyNote/vue/src/router/index.js
 * @Description: 路由整合
 */
import Vue from 'vue'
import Router from 'vue-router'
import routes from './routers'
import store from '../store'
import {
  getRouterStatus
} from "../libs/util"
Vue.use(Router)
const router = new Router({
  routes,
  mode: 'history',
  scrollBehavior: (to, from, savedPosition) => {

    // if (savedPosition) {
    //   return savedPosition;
    // } else {
      if (store.getters.getPageCache.includes(to.name)) {
        $("body,html").animate({
          scrollTop: store.state.app.pageCacheScroll[to.name]
        }, 0);
      } else {
        $("body,html").animate({
          scrollTop: 0
        }, 0);
      }
    // }

  }
})
let routerStatus = getRouterStatus(routes);
store.commit("setHideNavs", routerStatus.hideNavs)
store.commit("setHideBackTops", routerStatus.hideBackTops)
store.dispatch("APageCache", routerStatus.pageCache)
router.beforeEach((to, from, next) => {
  if (store.getters.getPageCache.includes(from.name)) {
    store.commit("setPageCacheScrollItem", {
      name: from.name,
      scroll: $(window).scrollTop()
    })
  }
  store.commit("setActiveNav", to.matched[0].name)
  next()
})

router.afterEach(to => {

  window.document.title = to.meta.title
})




export default router